package com.site.service.impl;

import com.common.exception.BusinessException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.site.mapper.BannerMapper;
import com.site.model.Banner;
import com.site.service.BannerService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by robin on 2016/12/14.
 * banner service impl
 */
@Service
public class BannerServiceImpl implements BannerService{
    @Autowired
    private BannerMapper mapper;

    /**
     * 保存banner
     * @param banner banner对象
     * @return 结果
     */
    @Override
    public int save(Banner banner) {
        if(StringUtils.isBlank(banner.getCate())) throw new BusinessException("请选择板块");
        if(StringUtils.isBlank(banner.getBannerImg())) throw new BusinessException("请选择图片");
        if(StringUtils.isBlank(banner.getBannerUrl())) banner.setBannerUrlEnable("0");
        else banner.setBannerUrlEnable("1");
        banner.setBannerTime(new Date());
        banner.setBannerEnable("1");
        banner.setSort((mapper.selectMaxSort()+1)+"");
        return mapper.insertSelective(banner);
    }


    /**
     * 删除信息
     * @param id 信息Id
     * @return 结果
     */
    @Override
    public int delete(String id) {
        Banner banner = new Banner();
        banner.setBannerEnable("0");
        banner.setBannerId(id);
        return mapper.updateByPrimaryKeySelective(banner);
    }

    /**
     * 修改信息
     * @param banner banner对象
     * @return 结果
     */
    @Override
    public int edit(Banner banner) {
        if(StringUtils.isBlank(banner.getCate())) throw new BusinessException("请选择板块");
        if(StringUtils.isBlank(banner.getBannerUrl())) banner.setBannerUrlEnable("0");
        else banner.setBannerUrlEnable("1");
        return mapper.updateByPrimaryKeySelective(banner);
    }

    /**
     * 查询信息
     * @param banner      参数
     * @param pageNum  页码
     * @param pageSize 页面大小
     * @return
     */
    @Override
    public PageInfo<Banner> search(Banner banner, int pageNum, int pageSize) {
//        PageHelper.startPage(pageNum, pageSize);
        List<Banner> list = mapper.search(banner);
        return new PageInfo<Banner>(list);
    }

    /**
     * 根据Id查询banner
     * @param id id
     * @return banner对象
     */
    @Override
    public Banner selectById(String id) {
        return mapper.selectByPrimaryKey(id);
    }


    /**
     * 向上调整信息位置
     * @param id   Id
     * @param sort 位置
     * @return 结果
     */
    @Override
    public int upSort(String id, int sort) {
        int maxSort = mapper.selectMaxSort();
        if(maxSort == sort) return 1;
        Map param = new HashMap();
        param.put("bySort",sort+1);param.put("sort",sort);
        mapper.updateBySort(param);
        param = new HashMap();
        param.put("sort",sort+1);param.put("id",id);
        mapper.updateSortById(param);
        return 1;
    }

    /**
     * 向下调整信息位置
     * @param id   Id
     * @param sort 位置
     * @return     结果
     */
    @Override
    public int downSort(String id, int sort) {
        int minSort = mapper.selectMinSort();
        if(minSort == sort) return 1;
        Map param = new HashMap();
        param.put("bySort",sort-1);param.put("sort",sort);
        mapper.updateBySort(param);
        param = new HashMap();
        param.put("sort",sort-1);param.put("id",id);
        mapper.updateSortById(param);
        return 1;
    }

    /**
     * 根据板块查询banner
     * @param cate 板块
     * @return 结果
     */
    @Override
    public List<Banner> selectByCate(String cate) {
        return mapper.selectByCate(cate);
    }
}
